package com.atguigu03.threadsafe.notsafe;

/**
 * @author NoobMaster69  * @date 2024/6/12 0012 星期三 17:05
 */

class Window implements Runnable {
    int ticket = 500;
    static boolean isFlag = true;
    @Override
    public void run() {
        while (isFlag) {
            show();
        }
    }

    public synchronized void show() {
        if (ticket > 0) {
            System.out.println(Thread.currentThread().getName() + "当前票为：" + ticket);
            ticket--;
        } else {
            isFlag = false;
        }
    }
}

public class WindowTest {
    public static void main(String[] args) {
        Window w = new Window();

        Thread t1 = new Thread(w);
        Thread t2 = new Thread(w);
        Thread t3 = new Thread(w);

        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");

        t1.start();
        t2.start();
        t3.start();
    }
}
